import { _decorator, Component, EventKeyboard, input, Input, KeyCode, Node, Quat, RigidBody, tween, Vec3 } from 'cc';
const { ccclass, property } = _decorator;

@ccclass('MovingWheel')
export class MovingWheel extends Component {
    rigid: RigidBody = null;
    start() {
        
        this.rigid = this.getComponent(RigidBody);
        // this.rigid.useGravity = true
        input.on(Input.EventType.KEY_PRESSING, this.keydown, this);
        input.on(Input.EventType.KEY_DOWN, this.keydown, this);
        input.off(Input.EventType.KEY_UP, this.offMovingWheelByTween, this);
    }

    //施加一个外力
    keydown(event: EventKeyboard){
        console.log("node name ", this.node.name)

        console.log("keydown: event.keyCode = ", event.keyCode, "KeyCode.KEY_Z =  ", KeyCode.KEY_Z )
        if(event.keyCode === KeyCode.KEY_Z){
            this.rigid.applyForce(new Vec3(0, 0, 200));//对刚体施加作用力
            //这里可以判断那个轮子可以动那个不能动
            // this.onMovingWheelByTween(this.node)

            //Configurable

        }
    }

    //轮胎转动
    onMovingWheelByTween(wheel: Node) {
        console.log("wheel ", wheel.name)

        let tweenDuration : number = 0.1;
        let  quat : Quat = new Quat();
        Quat.fromEuler(quat, 90, 0, 0);
        tween(wheel)
            // .to(tweenDuration, { 
            //     // scale: new Vec3(1.2, 3, 1),                     // 缩放缓动
            //     rotation:quat }                                 // 旋转缓动
            // )
            .by(tweenDuration, { eulerAngles: new Vec3(-180, 0, 0) })
            // .to(tweenDuration, { eulerAngles: new Vec3(180, 0, 0) })
            // .repeat(3)                                   
            .start();

    }
    offMovingWheelByTween(){
        let thatNode = this.node
        tween(thatNode).stop()
    }

    update(deltaTime: number) {

    }
}

